import 'package:bilibili/pages/main/main.dart';
import 'package:dio/dio.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:bilibili/core/extension/int_extension.dart';
import 'package:bilibili/core/http/http_request.dart';

class LoginBottom {
  final mainColor = Colors.pinkAccent;
  final fixedSize = Size(325.px, 70.px);
  final BuildContext context;
  final String account;
  final String password;

  LoginBottom(this.context, this.account, this.password);

  Widget buildLoginBottom({bool isReg = false, bool disabled = true}) {
    return isReg ? buildRegRow(context, disabled) : buildLoginRow(context);
  }

  Row buildLoginRow(BuildContext context) {
    return Row(
      mainAxisAlignment: MainAxisAlignment.spaceAround,
      children: [
        OutlinedButton(
          onPressed: () {
            Navigator.of(context).pushNamed("/register");
          },
          child: Text(
            "注册",
            style: TextStyle(color: mainColor),
          ),
          style: OutlinedButton.styleFrom(
              side: BorderSide(color: mainColor), fixedSize: fixedSize),
        ),
        TextButton(
            onPressed: () {},
            child: Text(
              "登录",
              style: TextStyle(color: Colors.white),
            ),
            style: TextButton.styleFrom(
                backgroundColor: mainColor, fixedSize: fixedSize))
      ],
    );
  }

  Widget buildRegRow(BuildContext context, bool disabled) {
    return Padding(
      padding: EdgeInsets.fromLTRB(10.px, 20.px, 10.px, 20.px),
      child: TextButton(
          onPressed: disabled
              ? null
              : () async {
                  Map map = Map();
                  map['account'] = account;
                  map['password'] = password;
                  Map message = {};
                  HttpRequest.jsonPost(
                          "http://192.168.0.107:5000/api/register", map)
                      .then((value) => message = value);

                  Navigator.of(context).pushNamed(MainScreen.routName, arguments: message["is_login"]);

          },
          child: Text(
            "注册",
            style: TextStyle(color: Colors.white),
          ),
          style: TextButton.styleFrom(
            backgroundColor:
                disabled ? Color.fromRGBO(255, 0, 50, 0.3) : mainColor,
            minimumSize: Size(double.infinity, 80.px),
            padding: EdgeInsets.fromLTRB(20.px, 0, 20.px, 0),
          )),
    );
  }
}
